#include "widget.h"
#include "ui_widget.h"
#include <QTime>
#include <QMetaType>
#include <QDebug>
#include "Qt_Halcon.h"
#include <QMessageBox>
#include <QCloseEvent>
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    qRegisterMetaType<HTuple>("HTuple");
    qRegisterMetaType<HObject>("HObject");
    isContinue = false;
    visentechTop = new Visentech();
    visentechTop->start();
    connect(visentechTop,&Visentech::send_ObjectModel3D,this,&Widget::receive_ObjectModel3D);
    connect(visentechTop,&Visentech::send_ImageL,this,&Widget::receive_ImageL);
    connect(visentechTop,&Visentech::send_ImageR,this,&Widget::receive_ImageR);
    Hlong MainWindowID_ObjectModel3D;
    MainWindowID_ObjectModel3D = ui->widget_ObjectModel3D->winId();
    OpenWindow(0,0,ui->widget_ObjectModel3D->width(),ui->widget_ObjectModel3D->height(),MainWindowID_ObjectModel3D,"visible","",&ObjectModel3D_Handle);
    HDevWindowStack::Push(ObjectModel3D_Handle);
    Hlong MainWindowID_ImageL;
    MainWindowID_ImageL = ui->widget_L->winId();
    OpenWindow(0,0,ui->widget_L->width(),ui->widget_L->height(),MainWindowID_ImageL,"visible","",&ImageL_Handle);
    HDevWindowStack::Push(ImageL_Handle);
    Hlong MainWindowID_ImageR;
    MainWindowID_ImageR = ui->widget_R->winId();
    OpenWindow(0,0,ui->widget_R->width(),ui->widget_R->height(),MainWindowID_ImageR,"visible","",&ImageR_Handle);
    HDevWindowStack::Push(ImageR_Handle);

}
Widget::~Widget()
{
    delete visentechTop;
    delete ui;
}
//接受点云的槽函数
void Widget::receive_ObjectModel3D(HTuple ObjectModel3D)
{
    isContinue = true;
    HTuple hv_Status, hv_PoseOut;
    visualize_object_model_3d(ObjectModel3D_Handle, ObjectModel3D, HTuple(), HTuple(),
        (HTuple("lut").Append("color_attrib")), (HTuple("color1").Append("coord_z")),
        HTuple(), HTuple(), HTuple(), &hv_PoseOut);
     isContinue = false;
}
//接受左相机图像
void Widget::receive_ImageL(HObject Image)
{
    HTuple  hv_Width, hv_Height;
    GetImageSize(Image, &hv_Height, &hv_Width);
    SetPart(ImageL_Handle,0, 0, hv_Width-1, hv_Height-1);
    DispObj(Image,ImageL_Handle);

}
//接受右相机图像
void Widget::receive_ImageR(HObject Image)
{
    HTuple  hv_Width, hv_Height;
    GetImageSize(Image, &hv_Height, &hv_Width);
    SetPart(ImageR_Handle,0, 0, hv_Width-1, hv_Height-1);
    DispObj(Image,ImageR_Handle);
    qDebug()<<"receive_ImageR";
}
//初始化设备
void Widget::on_btn_Init_clicked()
{
     visentechTop->Init();
}
//连接设备
void Widget::on_btn_connect_clicked()
{
     visentechTop->Connect();
}
//开始扫描
void Widget::on_btn_scan_clicked()
{
    if(isContinue == true)
    {
       QMessageBox::warning(this,"warning","Please click the continuing button!");
    }
    else {
       visentechTop->StartAcquire();
       isContinue = false;
    }
}
//保存点云
void Widget::on_btn_save_Pointcloud_clicked()
{
    QString filename = "ObjectModel3D.om3";
    visentechTop->SaveColudPoints(filename);
}
//保存图像
void Widget::on_btn_save_Image_clicked()
{
    const char* filenameL = "L.bmp";
    visentechTop->SaveImage(filenameL);
    const char* filenameR = "R.bmp";
    visentechTop->SaveImage(filenameR,false);
}
//关闭设备
void Widget::on_btn_close_clicked()
{
   visentechTop->Uninit();
}
//关闭窗口
void Widget::closeEvent(QCloseEvent *event)
{
    if(isContinue == true)
    {
        int r=QMessageBox::warning(this,tr("warning"),tr("Forgot to click the continu button,Are you sure you want to quit?"),QMessageBox::Yes|QMessageBox::Default,QMessageBox::No|QMessageBox::Escape);
           if (r==QMessageBox::Yes)
               event->accept();
           else
               event->ignore();
    }
    visentechTop->Uninit();
}
